#ifndef COMMON_UNIT_RAND_H
#define COMMON_UNIT_RAND_H


#include <vector>
#include "../Type.h"
#include "../Time.h"

class MyRand
{
public :
	~MyRand( ){ }
	static MyRand& Instance(){
		static MyRand __instance;
		return __instance;
	}

	void SRand()
	{
		uint milliSec = GetTime();
		srand( milliSec ) ;
	}

	int Rand( )
	{
		int num = rand();
		return num;
	}
	// [0 ,max]
	int           Rand( int num) {
		if(num==0) return 0; 
		int rNum=Rand(); 
		return rNum % (num + (num>0? 1: -1) ); 
	}
	// [min, max]
	int Rand( int min, int max){
		int rNum=Rand(max>min?max-min:min-max); 
		return rNum + (max>min? min: max) ;
	}

	void Rand( std::vector<int>& randlist, uint total, int max, int min = 0 )
	{
		randlist.clear();
		for ( uint i = 0; i < total; i++ )
		{
			int tempSeed = (int)rand();
			randlist.push_back(tempSeed % ( (max>min)?(max-min):(min-max) + 1 ) + max>min? min: max);
		}
	}

private:
	MyRand( ) {
		MyRand::SRand();
	}
};

inline int Rand() { return MyRand::Instance().Rand();}
inline int Rand(int max) { return MyRand::Instance().Rand(max);}
inline int Rand(int min,int max) { return MyRand::Instance().Rand(min,max);}
inline void Rand( std::vector<int>& randlist, uint total, int max, int min = 0 ) { return MyRand::Instance().Rand(randlist, total, max, min); }

#endif
